package admin.forms;

import java.net.URL;
import java.util.TreeMap;

import javax.servlet.http.HttpServletRequest;

import models.AutoSite;

public class AutoSiteForm {
	public static TreeMap checkValid(HttpServletRequest request, AutoSite autoSite) {
		TreeMap form = new TreeMap();
		TreeMap checkValid = new TreeMap();
		
		String site = (String) request.getParameter("site");
		String blogId = (String) request.getParameter("blogId");
		String regLink = (String) request.getParameter("regLink");
		String regTitle = (String) request.getParameter("regTitle");
		String regDescription = (String) request.getParameter("regDescription");
		String regRemove = (String) request.getParameter("regRemove");
		String currentPage = (String) request.getParameter("currentPage");
		String totalPage = (String) request.getParameter("totalPage");
		String sitePage = (String) request.getParameter("sitePage");
		String scheduleSite = (String) request.getParameter("scheduleSite");
		String scheduleSitePage = (String) request.getParameter("scheduleSitePage");
		String typeSource = (String) request.getParameter("typeSource");
		String category = (String) request.getParameter("category");
		String published = (String) request.getParameter("published");
		String displaySource = (String) request.getParameter("displaySource");
		
		if (site == null || site.equals("")) {
			checkValid.put("site", "Site is not empty!");
		} else {
			try {
				URL surl = new URL(site);
			} catch (Exception e) {
				checkValid.put("site", "Site is not url valid!");
			}
			autoSite.setSite(site);
		}
		if (blogId == null || blogId.equals("")) {
			checkValid.put("blogId", "BlogId is not empty!");
			autoSite.setBlogId("");
		} else {
			autoSite.setBlogId(blogId);
		}
		
		if (regLink == null || regLink.equals("")) {
			checkValid.put("regLink", "Title link is not empty!");
		} else {
			autoSite.setRegLink(regLink);
		}
		
		if (regTitle == null || regTitle.equals("")) {
			checkValid.put("regTitle", "Title is not empty!");
		} else {
			autoSite.setRegTitle(regTitle);
		}
		
		if (regDescription == null || regDescription.equals("")) {
			checkValid.put("regDescription", "Content is not empty!");
			autoSite.setRegDescription("");
		} else {
			autoSite.setRegDescription(regDescription);
		}
			
		if (regRemove != null && !regRemove.equals(""))
			autoSite.setRegRemove(regRemove);
		
		if (currentPage == null || currentPage.equals("")) {
			checkValid.put("currentPage", "Current page is not empty!");
		} else {
			try {
				autoSite.setCurrentPage(Integer.parseInt(currentPage));
			} catch (Exception e) {
				checkValid.put("currentPage", "Current page must be a number!");
			}
		}
		
		if (totalPage == null || totalPage.equals("")) {
			checkValid.put("totalPage", "Total page is not empty!");
		} else {
			try {
				autoSite.setTotalPage(Integer.parseInt(totalPage));
			} catch (Exception e) {
				checkValid.put("totalPage", "Total page must be a number!");
			}
		}
		
		if (sitePage == null || sitePage.equals("")) {
			checkValid.put("sitePage", "Site pages is not empty!");
			autoSite.setSitePage("");
		} else {
			autoSite.setSitePage(sitePage);
		}
		
		if (typeSource == null || typeSource.equals("")) {
			checkValid.put("typeSource", "Type Source is not null or empty!");
		} else {
			autoSite.setTypeSource(typeSource);
		}
		
		if (published != null)
			autoSite.setPublished(new Boolean(published));
		
		if (displaySource != null)
			autoSite.setDisplaySource(new Boolean(displaySource));
		
		if (scheduleSite != null && !scheduleSite.equals("")) {
			try {
				autoSite.setScheduleSite(Integer.parseInt(scheduleSite));
			} catch (Exception e) {
				checkValid.put("scheduleSite", "Time schedule must be a number!");
			}
		} else {
			checkValid.put("scheduleSite", "Time schedule must be a number!");
		}
			
		if (category != null && !category.equals(""))
			autoSite.setCategory(category);
		
		
		if (scheduleSitePage == null || scheduleSitePage.equals("")) {
			checkValid.put("scheduleSitePage", "Time schedule page is not empty!");
		} else {
			try {
				autoSite.setScheduleSitePage(Integer.parseInt(scheduleSitePage));
			} catch (Exception e) {
				checkValid.put("scheduleSitePage", "Time schedule page must be a number!");
			}
		}
		
		if (sitePage == null || sitePage.equals("")) {
			checkValid.put("sitePage", "Site pages is not empty!");
		} else {
			autoSite.setSitePage(sitePage);
		}
		
		if (currentPage == null || currentPage.equals("")) {
			checkValid.put("currentPage", "Current page is not empty!");
		} else {
			try {
				autoSite.setCurrentPage(Integer.parseInt(currentPage));
			} catch (Exception e) {
				checkValid.put("currentPage", "Current page must be a number!");
			}
		}
		
		if (totalPage == null || totalPage.equals("")) {
			checkValid.put("totalPage", "Total page is not empty!");
		} else {
			try {
				autoSite.setTotalPage(Integer.parseInt(totalPage));
			} catch (Exception e) {
				checkValid.put("totalPage", "Total page must be a number!");
			}
		}
		
		form.put("checkValid", checkValid);
		form.put("autoSite", autoSite);
		return form;
	}
	
}
